package com.lyshop.jinding.utils;

import android.app.Activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.OnPermissionInterceptor;
import com.hjq.permissions.PermissionFragment;
import com.hjq.permissions.XXPermissions;
import com.lyshop.jinding.view.PermissionDialog;

import java.util.ArrayList;
import java.util.List;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/XXPermissions
 *    time   : 2021/01/04
 *    desc   : 权限申请拦截器
 */
public final class PermissionInterceptor implements OnPermissionInterceptor {

    @Override
    public void launchPermissionRequest(@NonNull Activity activity, @NonNull List<String> allPermissions, @Nullable OnPermissionCallback callback) {
        List<String> deniedPermissions = XXPermissions.getDenied(activity, allPermissions);
        String permissionString = PermissionNameConvert.getPermissionString(activity, deniedPermissions);
        PermissionDialog dialog = new PermissionDialog(activity, new PermissionDialog.PrivateConfirmListener() {
            @Override
            public void onCancelListener() {
                if (callback != null) {
                    callback.onDenied(deniedPermissions, false);
                }
            }

            @Override
            public void onConfirmListener() {
                PermissionFragment.launch(activity, new ArrayList<>(allPermissions), PermissionInterceptor.this, callback);
            }
        });
        dialog.show();

//        // 这里的 Dialog 只是示例，没有用 DialogFragment 来处理 Dialog 生命周期
//        new AlertDialog.Builder(activity)
//                .setTitle("授权提示")
//                .setMessage(activity.getString(R.string.common_permission_message, permissionString))
//                .setPositiveButton("授权", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        PermissionFragment.launch(activity, new ArrayList<>(allPermissions), PermissionInterceptor.this, callback);
//                        dialog.dismiss();
//
//                    }
//                })
//                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        dialog.dismiss();
//                        if (callback != null) {
//                            callback.onDenied(deniedPermissions, false);
//                        }
//                    }
//                })
//                .show();
    }
}